package info.nixbear.tgcms.services.dao;

import info.nixbear.tgcms.domain.Attachment;
import info.nixbear.tgcms.domain.EntityBase;
import info.nixbear.tgcms.domain.Page;
import info.nixbear.tgcms.domain.Redirect;
import info.nixbear.tgcms.domain.Template;
import info.nixbear.tgcms.services.dao.appengine.AppengineEntityDao;
import info.nixbear.tgcms.services.dao.appengine.AppenginePageDao;

import com.google.web.bindery.requestfactory.shared.ServiceLocator;

public class EntityServiceLocator implements ServiceLocator {
	
	@Override
	@SuppressWarnings("unchecked")
	public EntityDao<? extends EntityBase> getInstance(Class<?> clazz) {
		if(PageDao.class.equals(clazz) || Page.class.equals(clazz)) {
			return new AppenginePageDao();
		}
		if(RedirectDao.class.equals(clazz) || Redirect.class.equals(clazz)) {
			return new AppengineEntityDao<Redirect>(Redirect.class);
		}
		if(TemplateDao.class.equals(clazz) || Template.class.equals(clazz)) {
			return new AppengineEntityDao<Template>(Template.class);
		}
		if(Attachment.class.equals(clazz)) {
			return new AppengineEntityDao<Attachment>(Attachment.class);
		}
		try {
			return (EntityDao<? extends EntityBase>) clazz.newInstance();
		} catch (InstantiationException e) {
			throw new RuntimeException(e);
		} catch (IllegalAccessException e) {
			throw new RuntimeException(e);
		}
	}
}